//
//  MultipeerConnectivityAssistant.m
//  GoodsBTS
//
//  Created by onemade on 14/9/22.
//  Copyright (c) 2014年 CNPC. All rights reserved.
//

#import "MultipeerConnectivityAssistant.h"

@implementation MultipeerConnectivityAssistant

+ (MultipeerConnectivityAssistant *)sharedService {
    static dispatch_once_t onceToken;
    static MultipeerConnectivityAssistant *sharedService;
    dispatch_once(&onceToken, ^{
        sharedService = [[self alloc] init];
    });
    
    return sharedService;
}

- (void)sendMessage:(NSData *)data error:(NSError **)error {
    [self.dataSession sendData:data toPeers:[self.dataSession connectedPeers] withMode:MCSessionSendDataReliable error:error];
}
#pragma mark - MCNEARBYSERVICEBADVERTISER
-(void)startAdvertiserWithName:(NSString *)name
{
    self.advertiserPeerID = [[MCPeerID alloc] initWithDisplayName:name];
    self.advertiser = [[MCNearbyServiceAdvertiser alloc] initWithPeer:self.advertiserPeerID discoveryInfo:nil serviceType:svcType];
    self.advertiser.delegate = self;
    
    self.dataSession = [[MCSession alloc] initWithPeer:self.advertiserPeerID];
    self.dataSession.delegate = self;
    
    [self.advertiser startAdvertisingPeer];
}
-(void)stopAdvertising
{
    [self.advertiser stopAdvertisingPeer];
}

#pragma mark - MCNearByServiceAdvertiserDelegate
-(void)advertiser:(MCNearbyServiceAdvertiser *)advertiser didReceiveInvitationFromPeer:(MCPeerID *)peerID withContext:(NSData *)context invitationHandler:(void (^)(BOOL, MCSession *))invitationHandler
{
    invitationHandler(YES,self.dataSession);
}

-(void)advertiser:(MCNearbyServiceAdvertiser *)advertiser didNotStartAdvertisingPeer:(NSError *)error
{
    NSLog(@"%@",error);
}

#pragma mark - MCNEARBYSERVICEBROWSER
-(void)startBrowserWithName:(NSString *)name
{
    self.browserPeerID = [[MCPeerID alloc] initWithDisplayName:name];
    self.browser = [[MCNearbyServiceBrowser alloc] initWithPeer:self.browserPeerID serviceType:svcType];
    self.browser.delegate = self;
    self.dataSession = [[MCSession alloc] initWithPeer:self.browserPeerID];
    self.dataSession.delegate =self;
    
    [self.browser startBrowsingForPeers];
}
-(void)stopBrowser
{
    [self.browser stopBrowsingForPeers];
}

#pragma mark - MCNearByServiceBrowserDelegate
-(void)browser:(MCNearbyServiceBrowser *)browser foundPeer:(MCPeerID *)peerID withDiscoveryInfo:(NSDictionary *)info
{
    [browser invitePeer:peerID toSession:self.dataSession withContext:nil timeout:100000.0];
}
-(void)browser:(MCNearbyServiceBrowser *)browser lostPeer:(MCPeerID *)peerID
{
    
}

#pragma mark - MCSessionDelegate
-(void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state
{
    if ([_delegate respondsToSelector:@selector(didChangeState:forPeer:)]) {
        [_delegate didChangeState:state forPeer:peerID];
    }
//    NSString * status = nil;
//    switch (state) {
//        case MCSessionStateConnected:
//            status = @"Connected";
//            break;
//        case MCSessionStateConnecting:
//            status=@"Connecting";
//            break;
//        case MCSessionStateNotConnected:
//            status = @"DisConnected";
//            break;
//            
//        default:
//            break;
//    }
//    
//    NSLog(@"%@",status);
}
-(void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID
{
//    NSString * message = [[NSString alloc ] initWithData:data encoding:NSUTF8StringEncoding];
//    UIImage *image = [UIImage imageWithData:data];
//    
    if ([_delegate respondsToSelector:@selector(didReceiveData:fromPeer:)]) {
        [_delegate didReceiveData:data fromPeer:peerID];
    }
}
-(void)session:(MCSession *)session didReceiveStream:(NSInputStream *)stream withName:(NSString *)streamName fromPeer:(MCPeerID *)peerID
{
    
}
-(void)session:(MCSession *)session didFinishReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID atURL:(NSURL *)localURL withError:(NSError *)error
{
    
}
-(void)session:(MCSession *)session didStartReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID withProgress:(NSProgress *)progress
{
    
}
-(void)session:(MCSession *)session didReceiveCertificate:(NSArray *)certificate fromPeer:(MCPeerID *)peerID certificateHandler:(void (^)(BOOL))certificateHandler
{
    certificateHandler(YES);
}
@end
